package com.ruoyi.web.controller.app;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.system.domain.AppInstaller;
import com.ruoyi.system.service.AppInstallerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = "用户端:版本相关接口")
@RestController
@RequestMapping("public/version")
public class VersionController extends AppController {
    @Autowired
    private AppInstallerService appInstallerService;


    @ApiOperation(value = "检查更新")
    @GetMapping("checkUpdate")
    public ResponseResult<AppInstaller> getVersion(String platform, String version) {
//        log.info("前端版本号为:{}", version);
        AppInstaller appInstaller = appInstallerService.getById(1L);
        if (StrUtil.equals(version, appInstaller.getVersion())) {
            return this.setSuccessResult();
        }
        Map<String, String> map = new HashMap<>();
        map.put("version_name", appInstaller.getVersion());
        map.put("installer_url", appInstaller.getInstallerUrl());
        map.put("patch_url", appInstaller.getPatchUrl());
        map.put("update_desc", appInstaller.getUpdateDesc());
        map.put("update_level", appInstaller.getUpdateLevel());
        return this.setSuccessResult(map);
    }

//
//    private void test() {
//
//        AppInstaller appInstaller = appInstallerService.lambdaQuery()
//                .eq(AppInstaller::getPlatform, platform).one();
//
//
//        String version = "";
//        String _version = appInstaller.getVersion();
//        if (Objects.isNull(appInstaller) || VersionComparator.INSTANCE.compare(version, _version) >= 0) {
//            return JsonFormat.successFormat("无更新");
//        }
//
//        List<String> versionList = Splitter.on(".")
//                .omitEmptyStrings()
//                .trimResults()
//                .splitToList(version)
//                .stream()
//                .collect(Collectors.toList());
//        List<String> _versionList = Splitter.on(".")
//                .omitEmptyStrings()
//                .trimResults()
//                .splitToList(_version)
//                .stream()
//                .collect(Collectors.toList());
//
//
//        String V = "X";
//        if (_versionList.get(0).equals(versionList.get(0))) {
//            if (_versionList.get(1).equals(versionList.get(1))) {
//                if (!_versionList.get(2).equals(versionList.get(2))) {
//                    V = "Z";
//                }
//            } else {
//                V = "Y";
//            }
//        }
//
//        return JsonFormat.successFormat(
//                ImmutableMap.builder().put("update_level", V)
//                        .put("update_desc", appInstaller.getUpdate_desc())
//                        .put("installer_url", V.equals("X") ? appInstaller.getInstaller_url() : appInstaller.getPatch_url())
//                        .put("version_name", _version)
//                        .build()
//        );
//    }
}
